package com.hsnn.medstgmini.pro.project.controller;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.dict.dao.DicExperttypeDao;
import com.hsnn.medstgmini.base.dict.model.DicExperttype;
import com.hsnn.medstgmini.base.std.model.StdExpert;
import com.hsnn.medstgmini.base.std.service.StdExpertManager;
import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.pro.project.dao.BidProjectDao;
import com.hsnn.medstgmini.pro.project.model.BidExpertExtractListRuleList;
import com.hsnn.medstgmini.pro.project.model.BidExpertExtractRule;
import com.hsnn.medstgmini.pro.project.model.BidExpertExtractRuleListFormLog;
import com.hsnn.medstgmini.pro.project.model.BidProjExpert;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.model.form.BidExpertExtractRuleListForm;
import com.hsnn.medstgmini.pro.project.service.BidExpertExtractRuleManager;
import com.hsnn.medstgmini.sys.model.SysRole;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.sys.model.UserType;
import com.hsnn.medstgmini.sys.service.SysNoticeManager;
import com.hsnn.medstgmini.sys.service.SysRoleManager;
import com.hsnn.medstgmini.sys.service.SysUserManager;
import com.hsnn.medstgmini.util.AccountUtil;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("expertExtractRule")
public class BidExpertExtractRuleController {
	
	private static final Logger log = Logger.getLogger(BidExpertExtractRuleController.class);
	@Autowired
	private BidExpertExtractRuleManager bidExpertExtractManager;
	
	@Autowired
	private BidProjectDao bidProjectDao;
	
	@Autowired
	private DicExperttypeDao dicExperttypeDao;
	
	@Autowired
	private SysNoticeManager sysNoticeManager;
	
	@Autowired
	private HttpServletRequest request;

	@Autowired
	private SysUserManager userManager;

	@Autowired
	private SysRoleManager roleManager;
	
	@Autowired
	private StdExpertManager expertManager;
	


	
	
	/**
	 * @category 跳转到规则详情页面
	 * @author zhouqi
	 * @date 2015年9月1日09:05:00
	 * @param Model model
	 * @return String
	 */
	@RequestMapping("toExpertExtractRuleDetail")
	public String toExpertExtractRuleDetail(String ruleId, Model model){
		if(StringUtils.isNoneBlank(ruleId)){
			BidExpertExtractRule rule = bidExpertExtractManager.getRuleById(ruleId);
			String updDateTime = DateUtil.format(rule.getUpdDateTime(), "yyyy-MM-dd HH:mm:ss");
			String projId = rule.getProjId();
			String expertTypeId = rule.getExpertTypeId();
			model.addAttribute("projName",bidProjectDao.getBidProjectById(projId).getName());
			model.addAttribute("expertTypeName",bidExpertExtractManager.getDicExperttypeById(expertTypeId).getExpertTypeName());
			model.addAttribute("ruleId", ruleId);
			model.addAttribute("updDateTime", updDateTime);
			model.addAttribute("rule", rule);
			model.addAttribute("projId", projId);
		}
		return "/pro/project/bidExpertExtractRuleDetail";
	}
	
	/**
	 * @category 跳转到维护记录详情页面
	 * @author zhouqi
	 * @date 2015年9月1日09:05:00
	 * @param Model model
	 * @return String
	 */
	@RequestMapping("toExpertExtractRuleLogDetail")
	public String toExpertExtractRuleLogDetail(String ruleId,String updDateTime, Model model){
		BidExpertExtractRuleListFormLog oldLog = null;
		BidExpertExtractRuleListFormLog newLog = null;
		String oldIndusion = "";
		String newIndusion = "";
		String oldLogId = "";
		String newLogId = "";
		List<Map<String, Object>> newExpertType = null;
		List<Map<String, Object>> newHospType = null;
		List<Map<String, Object>> oldHospType = null;
	    try {
			List<BidExpertExtractRuleListFormLog> ruleList = bidExpertExtractManager.getRuleCompare(ruleId, updDateTime);
			if(ruleList.size()==1){
				oldLog = ruleList.get(0);
				oldIndusion = ruleList.get(0).getInfusion();
				oldLogId = ruleList.get(0).getRuleLogId();
			}
			if(ruleList.size()> 1){
				oldLog = ruleList.get(0);
				newLog = ruleList.get(1);
				oldIndusion = ruleList.get(0).getInfusion();
				newIndusion = ruleList.get(1).getInfusion();
				oldLogId = ruleList.get(0).getRuleLogId();
				newLogId = ruleList.get(1).getRuleLogId();
			}
			List<Map<String, Object>> oldExpertType = bidExpertExtractManager.getExpertName(oldLogId);//专家类型
			if("1".equals(oldIndusion)){
				oldHospType = bidExpertExtractManager.getHospType(oldLogId);//医疗机构类型
			}
			
			if(newLog != null){
			if("1".equals(newIndusion)){
				newHospType = bidExpertExtractManager.getHospType(newLogId);//医疗机构类型
			}
			newExpertType = bidExpertExtractManager.getExpertName(newLogId);//专家类型
			}
			model.addAttribute("oldLog", oldLog);
			model.addAttribute("newLog", newLog);
			model.addAttribute("oldExpertType", oldExpertType);
			model.addAttribute("oldHospType", oldHospType);
			model.addAttribute("newExpertType", newExpertType);
			model.addAttribute("newHospType", newHospType);
		} catch (Exception e) {
			e.printStackTrace();
		}
	    
		return "/pro/project/bidExpertExtractRuleLogDetail";
	}
	
	
	/**
	 * @category 跳转到被抽取专家列表
	 * @author zhouqi
	 * @param 
	 * @return String
	 */
	@RequestMapping("toExtractExpertList")
	public String toExtractExpertList(Model model){
		List<BidProject> projectList = sysNoticeManager.getProjectList();
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		model.addAttribute("projectList", projectList);
		String ruleId = request.getParameter("ruleId");
		String projId =  request.getParameter("projId");
		if(StringUtils.isNoneBlank(ruleId)){
			model.addAttribute("ruleId", ruleId);
			model.addAttribute("projectId", user.getDefaultProjId());
			return "/pro/project/bidProjExpertForRule";
		}
		if(StringUtils.isNotBlank(projId)){
			model.addAttribute("projId", projId);
			return "/pro/project/bidProjExpertList";
		}
		if(StringUtils.isBlank(user.getDefaultProjId())){
			
			return "/pro/project/bidExpertSwitch"; 
		}else{
			model.addAttribute("defaultProjId",user.getDefaultProjId());
			model.addAttribute("projId",user.getDefaultProjId());
		    return "/pro/project/bidProjExpertList";
		}
	}
	
	/**
	 * @category 跳转到被抽取专家详情页面
	 * @author zhouqi
	 * @date 2015年9月2日11:21:42
	 * @param 
	 * @return String
	 */
	@RequestMapping("toExtractExpertDetail")
	public String toExtractExpertDetail(Model model){
		String ruleId = request.getParameter("ruleId");
		String expertId = request.getParameter("expertId");
		String projId = request.getParameter("projId");
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		if(StringUtils.isNotBlank(expertId)){
			StdExpert stdExpert = expertManager.getStdExpertById(expertId);
			model.addAttribute("stdExpert", stdExpert);
			SysUser sysUser = userManager.getUserById(expertId);
			model.addAttribute("sysUser", sysUser);
		}
		if(StringUtils.isNoneBlank(ruleId)){
			model.addAttribute("ruleId", ruleId);
		}
		if(StringUtils.isNoneBlank(projId)){
			model.addAttribute("projId", projId);
		}
		model.addAttribute("defaultProjId",user.getDefaultProjId());
		return "/pro/project/extractExpertDetail";
	}
	
	/**
	 * @category 跳转到维护记录列表
	 * @author zhouqi
	 * @param 
	 * @return String
	 */
	@RequestMapping("toExtractRuleLogList")
	public String toExtractRuleLogList(Model model){
		String ruleId = request.getParameter("ruleId");
		String projId = request.getParameter("projId");
		if(StringUtils.isNoneBlank(ruleId)){
			model.addAttribute("ruleId", ruleId);
		}
		if(StringUtils.isNoneBlank(projId)){
			model.addAttribute("projId", projId);
		}
		return "/pro/project/bidExpertExtractRuleLogList";
	}
	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * @category 跳转到规则列表
	 * @author zhouqi
	 * @param 
	 * @return String
	 */
	@RequestMapping("toExpertExtractRuleList")
	public String toExpertExtractRuleList(Model model){
		List<BidProject> projectList = sysNoticeManager.getProjectList();
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		String projId = request.getParameter("projId");
		List<BidExpertExtractListRuleList> FirstLevelExpertTypelist = bidExpertExtractManager.getSuExpertTypeByFatherId("0");//获取一级专家类别
		model.addAttribute("projectList", projectList);
		model.addAttribute("FirstLevelExpertTypelist", FirstLevelExpertTypelist);
		Object expertTypeCount= bidExpertExtractManager.getExpertTypeRuleById();
		model.addAttribute("expertTypeCount", expertTypeCount.toString());
		if(StringUtils.isNotBlank(projId)){
			model.addAttribute("projId",projId);
			return "/pro/project/bidExpertExtractRuleList";//projId存在直接跳转到项目抽取规则列表
		}
		if(StringUtils.isBlank(user.getDefaultProjId())){
			model.addAttribute("projId", projId);
			return "/pro/project/bidExpertExtractSwitch";//跳转到项目选择页面
		}
		if(StringUtils.isNotBlank(user.getDefaultProjId())){
			model.addAttribute("defaultProjId",user.getDefaultProjId());
			model.addAttribute("projId", user.getDefaultProjId());
		}
		return "/pro/project/bidExpertExtractRuleList";
	}
	
	/**
	 * @category 编辑规则
	 * @Auther 徐国鹏
	 * @date 2015年10月13日 上午9:04:25
	 * @return String
	 */
	@RequestMapping("toExpertExtractEdit")
	@ResponseBody
	public Pagination toExpertExtractEdit(BidExpertExtractRuleListForm form){
		Pagination page = new Pagination();
		String ruleId = request.getParameter("ruleId");
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		form.setExecUser(user.getId());
		form.setExecDatetime(DateUtil.getTimeStamp(new Date()));
		form.setUpdUser(user.getId());
		form.setUpdDateTime(DateUtil.getTimeStamp(new Date()));
		try {
			if (StringUtils.isNotEmpty(ruleId)) {
				List<Map<String, Object>> list  = bidExpertExtractManager.getRuleByIdList(ruleId);//最大人数
				form.setMaxExtractNum(list.get(0).get("maxExtractNum").toString());
				form.setInfusion(list.get(0).get("infusion").toString());
				
				List<Map<String, Object>> list1 = bidExpertExtractManager.getHospRuleByIdList(ruleId);//医疗机构类型
				form.setHospTypeList(list1);
				
				List<Map<String, Object>> list2 = bidExpertExtractManager.getExpertRuleByIdList(ruleId);//专家类型
				//form.setExpertTypeList(list2);
				List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>();
				if(list2 != null && list2.size() > 0){
					for(int i=0; i < list2.size(); i++){
						String expertTypeId = list2.get(i).get("expertTypeId").toString();
						DicExperttype dicExperttype = bidExpertExtractManager.getDicExperttypeById(expertTypeId);
						Map<String, Object> map = new HashMap<String,Object>();	
						if(dicExperttype.getExpertTypeCode().length()==3){
							map.put("firstLevel", dicExperttype.getDicExperttypeId());
							map.put("secondLevel", "");
							map.put("ThiredLevel","");
						}
						if(dicExperttype.getExpertTypeCode().length()==5){
							map.put("firstLevel", dicExperttype.getFatherExperttypeId());
							map.put("secondLevel",dicExperttype.getDicExperttypeId());
							map.put("ThiredLevel","");
						}
						if(dicExperttype.getExpertTypeCode().length()==7){
							DicExperttype fatherType = bidExpertExtractManager.getDicExperttypeById(dicExperttype.getFatherExperttypeId());
							map.put("firstLevel", fatherType.getFatherExperttypeId());
							map.put("secondLevel",dicExperttype.getFatherExperttypeId());
							map.put("ThiredLevel",dicExperttype.getDicExperttypeId());
						}
						map.put("expertCount", list2.get(i).get("count").toString());
						map.put("bidExpertTypeId", list2.get(i).get("bidExpertTypeId").toString());
						list3.add(map);
					}
				}
				form.setExpertTypeList(list3);
				page.setSuccess(true);
				page.setRows(form);
				}
			}catch(Exception e){
				log.error("",e);
				page.setSuccess(false);
			}
			return page;
		}
	/**                                                                                          
	 * @category 保存专家抽取规则
	 * @Auther 徐国鹏
	 * @date 2015年10月9日 下午2:21:36
	 * @return Pagination
	 */
	@RequestMapping("toExpertExtractEditList")
	@ResponseBody
	public Pagination toExpertExtractEditList(BidExpertExtractRuleListForm form) {
		Pagination page = new Pagination();
		//String ruleId = request.getParameter("ruleId");
		SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
		String hList = request.getParameter("hList");
		String eList = request.getParameter("eList");
		try{
				if(StringUtils.isBlank(form.getRuleId())){
					form.setRuleId(UUID.randomUUID().toString());
					form.setType(0);
				}else{
					form.setType(1);
				}
				form.setLogId(UUID.randomUUID().toString());
				Gson gson = new Gson();
				Type type = new TypeToken<List<Map<String,Object>>>() {}.getType();
				List<Map<String,Object>> hosptype = gson.fromJson(hList, type);
				form.setHospTypeList(hosptype);
				List<Map<String,Object>> experttype = gson.fromJson(eList, type);
				form.setExpertTypeList(experttype);
				form.setUpdUser(user.getId());
				bidExpertExtractManager.saveRuleByIdList(form);//规则
				if(form.getInfusion()!= null && "1".equals(form.getInfusion())){
					bidExpertExtractManager.saveHospLevelList(form);//医疗机构类型
				}
				bidExpertExtractManager.saveExpertLevelList(form);//专家类型
			    page.setSuccess(true);

		}catch(Exception e){
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	
		/**
		 * @category 抽取规则设置--规则列表
		 * @author 徐国鹏
		 * @param 
		 * @return String
		 */
		@SuppressWarnings("unchecked")
		@RequestMapping("getExpertExtractRuleList")
		@ResponseBody
		public Pagination getExpertExtractRuleList(){
			Pagination page = new Pagination(request);
			String ruleId = request.getParameter("ruleId");
			String projId = request.getParameter("projId");
			String execStatus = request.getParameter("execStatus");
			String status = request.getParameter("status");
			String expertTypeId = request.getParameter("expertTypeId");
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			if(StringUtils.isNotBlank(ruleId)){
				page.getConditions().put("ruleId", ruleId);
			}
			/*if (StringUtils.isNotBlank(request.getParameter("projId"))) {
				page.getConditions().put("projId", request.getParameter("projId"));
			}*/
			if(StringUtils.isNotBlank(execStatus)){
				page.getConditions().put("execStatus", execStatus);
			}
			if(StringUtils.isNotBlank(status)){
				page.getConditions().put("status", status);
			}
			if(StringUtils.isNotBlank(user.getDefaultProjId())){
				page.getConditions().put("projId", user.getDefaultProjId());
			}else{
				if(StringUtils.isNotBlank(projId)){
					page.getConditions().put("projId", projId);
				}
			}
			if(StringUtils.isNotBlank(expertTypeId)){
				page.getConditions().put("expertTypeId", expertTypeId);
			}
			try{
				Pagination page1 = new Pagination(request);
				List<Map<String, Object>> ruleList = (List<Map<String, Object>>) bidExpertExtractManager.getExpertExtractRulePage(page).getRows();//规则列表
				if(ruleList!=null&&ruleList.size()>0){
					for(int i = 0;i<ruleList.size();i++){
						if(Integer.parseInt(ruleList.get(i).get("infusion").toString())==1){
							page1.getConditions().put("ruleId", ruleList.get(i).get("ruleId").toString());
							List<Map<String, Object>> hospTypeList = (List<Map<String, Object>>) bidExpertExtractManager.getHospTypeList(page1).getRows();//医疗机构
							((List<Map<String, Object>>) page.getRows()).get(i).put("hospTypeList",hospTypeList);
						}else{
							((List<Map<String, Object>>) page.getRows()).get(i).put("hospTypeList",new ArrayList<Map<String, Object>>());
						}
						
					}
				}
			}catch(Exception e){
				log.error("",e);
				page.setSuccess(false);
			}
			return page;
		}
		
		/**
		 * @category 抽取规则管理--类型（子表）
		 * @author 徐国鹏
		 * @param 
		 * @return String
		 */
		@RequestMapping("getExpertTypeRuleDetailList")
		@ResponseBody
		public Pagination getExpertTypeRuleDetailList(){
			Pagination page = new Pagination(request);
			String ruleId = request.getParameter("ruleId");
			String projId = request.getParameter("projId");
			String status = request.getParameter("status");
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			if(StringUtils.isNotBlank(ruleId)){
				page.getConditions().put("ruleId", ruleId);
			}
			if(StringUtils.isNotBlank(status)){
				page.getConditions().put("status", status);
			}
			if(StringUtils.isNotBlank(user.getDefaultProjId())){
				page.getConditions().put("projId", user.getDefaultProjId());
			}else{
				if(StringUtils.isNotBlank(projId)){
					page.getConditions().put("projId", projId);
				}
			}
			try{
				//BidExpertExtractRuleListForm form = new BidExpertExtractRuleListForm();
				
				//form.setExpertTypeList((List<Map<String, Object>>) page.getRows());
				//page = bidExpertExtractManager.getHospTypeList(page);//医疗机构
				//form.setHospTypeList((List<Map<String, Object>>) page.getRows());
				
				page = bidExpertExtractManager.getExperttypeRuleList(page);//专家类型
				page.setSuccess(true);
				//page.setRows(form);
			}catch(Exception e){
				log.error("",e);
				page.setSuccess(false);
			}
			return  page;
		}
		
		/**
		 * @category 抽取专家查看--查看专家
		 * @author 徐国鹏
		 * @param 
		 * @return String
		 */
		@SuppressWarnings("unchecked")
		@RequestMapping("getExpertExtractListRuleList")
		@ResponseBody
		public Pagination getExpertExtractListRuleList(){
			Pagination page = new Pagination(request);
			String ruleId = request.getParameter("ruleId");
			String projId = request.getParameter("projId");
			String execStatus = request.getParameter("execStatus");
			String status = request.getParameter("status");
			String expertTypeId = request.getParameter("expertTypeId");
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			if(StringUtils.isNotBlank(ruleId)){
				page.getConditions().put("ruleId", ruleId);
			}
			if(StringUtils.isNotBlank(execStatus)){
				page.getConditions().put("execStatus", execStatus);
			}
			if(StringUtils.isNotBlank(status)){
				page.getConditions().put("status", status);
			}
			if(StringUtils.isNotBlank(user.getDefaultProjId())){
				page.getConditions().put("projId", user.getDefaultProjId());
			}else{
				if(StringUtils.isNotBlank(projId)){
					page.getConditions().put("projId", projId);
				}
			}
			if(StringUtils.isNotBlank(expertTypeId)){
				page.getConditions().put("expertTypeId", expertTypeId);
			}
			/*try{
				bidExpertExtractManager.getExpertExtractRulePageList(page);
			}*/
			try{
				Pagination page1 = new Pagination(request);
				List<Map<String, Object>> ruleList = (List<Map<String, Object>>) bidExpertExtractManager.getExpertExtractRulePageList(page).getRows();
				if(ruleList!=null&&ruleList.size()>0){
					for(int i = 0;i<ruleList.size();i++){
						if(Integer.parseInt(ruleList.get(i).get("infusion").toString())==1){
							page1.getConditions().put("ruleId", ruleList.get(i).get("ruleId").toString());
							List<Map<String, Object>> hospTypeList = (List<Map<String, Object>>) bidExpertExtractManager.getHospTypeList(page1).getRows();//医疗机构
							((List<Map<String, Object>>) page.getRows()).get(i).put("hospTypeList",hospTypeList);
						}else{
							((List<Map<String, Object>>) page.getRows()).get(i).put("hospTypeList",new ArrayList<Map<String, Object>>());
						}
						
					}
				}
			}catch(Exception e){
				log.error("",e);
				page.setSuccess(false);
			}
			return  page;
		}
		
		/**
		 * @category 抽取专家查看--专家查看（子表）
		 * @author 徐国鹏
		 * @param 
		 * @return String
		 */
		@RequestMapping("getExpertTypeRuleList")
		@ResponseBody
		public Pagination getExpertTypeRuleList(){
			Pagination page = new Pagination(request);
			String ruleId = request.getParameter("ruleId");
			String projId = request.getParameter("projId");
			String status = request.getParameter("status");
			SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			if(StringUtils.isNotBlank(ruleId)){
				page.getConditions().put("ruleId", ruleId);
			}
			if(StringUtils.isNotBlank(status)){
				page.getConditions().put("status", status);
			}
			if(StringUtils.isNotBlank(user.getDefaultProjId())){
				page.getConditions().put("projId", user.getDefaultProjId());
			}else{
				if(StringUtils.isNotBlank(projId)){
					page.getConditions().put("projId", projId);
				}
			}
			try{
				//bidExpertExtractManager.getExpertTypeRuleList(page);
				page = bidExpertExtractManager.getExpertTypeRuleList(page);//专家类型
				page.setSuccess(true);
			}catch(Exception e){
				log.error("",e);
				page.setSuccess(false);
			}
			return  page;
		}


		
		
		/**
		 * @category 抽取专家	
		 * @author 徐国鹏
		 * @date 2015年8月31日13:31:43
		 * @param String ruleId
		 * @return Pagination
		 */
		@RequestMapping("extractExpert")
		@ResponseBody
		public Pagination extractExpert(String ruleId){
			Pagination page = new Pagination();
			SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
			String[] ruleIds = ruleId.split(",");
			List<BidExpertExtractRuleListForm> rulelist = new ArrayList<BidExpertExtractRuleListForm>();
			for(int i = 0; i< ruleIds.length; i++){
				rulelist.add((BidExpertExtractRuleListForm) bidExpertExtractManager.getRuleByIdList(ruleIds[i]));//最大几人
			}
			//判断是否有重复的类型
			Set<String> set = new HashSet<String>();
			for(int i = 0; i< rulelist.size();i++){
				set.add(rulelist.get(i).getExpertTypeId());
			}
			/*if(rulelist.size() > set.size()){
				page.setSuccess(false);
				page.setMsg("专家类型重复，不能够同时执行，请重新设置");
				return page;
			}*/
			//判断端是否已经抽取
			for(int i = 0; i< rulelist.size();i++){
				if(rulelist.get(i).getExecStatus()==1){
					page.setSuccess(false);
					page.setMsg("所选规则已抽取，请重新选择抽取规则");
					return page;
				}
			}
			try{
				//extract rule(规则)
				List<BidProjExpert> bidProjExpertList = bidExpertExtractManager.executeExtractRule(rulelist);//执行抽取规则
				for(int i= 0; i<bidProjExpertList.size();i++ ){
					bidProjExpertList.get(i).setUpdDateTime(DateUtil.getTimeStamp());
					bidProjExpertList.get(i).setUpdUser(user.getName());
					bidProjExpertList.get(i).setStatus(1);
					bidExpertExtractManager.saveProjExpert(bidProjExpertList.get(i));//保存项目专家关联
				}
				for(int i = 0; i < rulelist.size();i++){
					BidExpertExtractRuleListForm rule = rulelist.get(i);
					rule.setExecStatus(1);
					bidExpertExtractManager.saveExtractRule(rule);//保存规抽取规则
				}
				page.setSuccess(true);
			}catch(Exception e){
				e.printStackTrace();
				page.setSuccess(false);
			}
			return page;
		}
		
		/**
		 * @category 启用、停用 
		 * @Auther 徐国鹏
		 * @date 2015年10月15日 下午1:53:31
		 * @return Pagination
		 */
		@RequestMapping("switchStatuss")
		@ResponseBody
		public Pagination switchStatuss(BidExpertExtractRuleListForm data){
			Pagination page = new Pagination(request);
			SysUser user = (SysUser) request.getSession().getAttribute(Constants.USERINFO);
			data.setUpdUser(user.getId());
			try {
				if (data != null) {
					data.setLogId(UUID.randomUUID().toString());
					data.setUpdUser(user.getId());
					bidExpertExtractManager.switchStatus(data);
					page.setSuccess(true);
				}
			} catch (Exception e) {
				e.printStackTrace();
				page.setSuccess(false);
			}
			return page;
		}
		
		
		
		
		/**
		 * @category 获取规则日志列表
		 * @author zhouqi
		 * @param 
		 * @return String
		 */
		@RequestMapping("getExtractRuleLogList")
	    @ResponseBody
		public Pagination getExtractRuleLogList(Pagination page){
			String startPoint = request.getParameter("startPoint");
			String endPoint = request.getParameter("endPoint");
			String ruleId = request.getParameter("ruleId");
			if(StringUtils.isNoneBlank(startPoint)){
				page.getConditions().put("startPoint", startPoint);
			}
			if(StringUtils.isNoneBlank(endPoint)){
				page.getConditions().put("endPoint", endPoint);
			}
			if(StringUtils.isNoneBlank(ruleId)){
				page.getConditions().put("ruleId", ruleId);
			}
			try{
				bidExpertExtractManager.getExtractRuleLogList(page);
				page.setSuccess(true);
			}catch(Exception e){
				e.printStackTrace();
				page.setSuccess(false);
			}
			return page;
		}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	
	
	
	
	
	
	
	
	/**
	 * @category 获取被抽去专家列表
	 * @author zhouqi
	 * @param 
	 * @return String
	 */
	@RequestMapping("getExtractExpertList")
    @ResponseBody
	public Pagination getExtractExpertList(){
		Pagination page = new Pagination(request);
		  String name = request.getParameter("name");
		String ruleId = request.getParameter("ruleId");
		String projId = request.getParameter("projId");
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		if(StringUtils.isNoneBlank(name)){
			page.getConditions().put("name", name);
		}
		if(StringUtils.isNoneBlank(ruleId)){
			page.getConditions().put("ruleId", ruleId);
		}
		if(StringUtils.isNotBlank(user.getDefaultProjId())){
			page.getConditions().put("projId", user.getDefaultProjId());
		}else{
			if(StringUtils.isNotBlank(projId)){
				page.getConditions().put("projId", projId);
			}
		}
		try{
			bidExpertExtractManager.getExtractExpertList(page);
			page.setSuccess(true);
		}catch(Exception e){
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 改变状态
	 * @author zhouqi
	 * @param 
	 * @return String
	 */
	public Pagination changeStatus(){
		Pagination page = new Pagination();
		String status = request.getParameter("status");
		String ruleId = request.getParameter("ruleId");
		try{
			if(StringUtils.isNoneBlank(status)&&StringUtils.isNoneBlank(ruleId)){
				BidExpertExtractRuleListForm rule = (BidExpertExtractRuleListForm) bidExpertExtractManager.getRuleByIdList(ruleId);
				rule.setStatus(Integer.parseInt(status));
				bidExpertExtractManager.saveExtractRule(rule);
			}
			page.setSuccess(true);
		}catch(Exception e){
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 获取或抽取专家数量
	 * @author 邱磊
	 * @param 
	 * @return int
	 */
	@RequestMapping("getFreeExpertCount")
	@ResponseBody
	public int getFreeExpertCount(BidExpertExtractRule rule){
		int expetCount = bidExpertExtractManager.getAllSubExpertCountByTypeId(rule.getExpertTypeId());
		return expetCount;
	}
	
	
	/**
	 * @category 根据专家类别id获取该类别专家数量	
	 * @author zhouqi
	 * @date 2015年8月31日13:31:43
	 * @param String overId
	 * @return Pagination
	 */
	@RequestMapping("getExpertCount")
	@ResponseBody
	public Pagination getExpertCount(String expertTypeId){
		Pagination page = new Pagination();
		try{
			Integer count = bidExpertExtractManager.getExpertCountByType(expertTypeId);
			page.setRows(count);
			page.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category 改变状态
	 * @date 2015年8月31日13:31:43
	 * @param String overId
	 * @return Pagination
	 */
	@RequestMapping("changeRuleStatus")
	@ResponseBody
	public Pagination changeRuleStatus(String overId, String status){
		Pagination page = new Pagination();
		try{
			String[] ruleIds = overId.split(",");
			for(int i = 0; i < ruleIds.length; i++){
				String ruleId = ruleIds[i];
				BidExpertExtractRuleListForm rule = (BidExpertExtractRuleListForm) bidExpertExtractManager.getRuleByIdList(ruleId);
				rule.setStatus(Integer.parseInt(status));
				bidExpertExtractManager.saveExtractRule(rule);	
			}
			page.setSuccess(true);
		}catch(Exception e){
			e.printStackTrace();
			page.setSuccess(false);
		}
		return page;
	}
	
	/**
	 * @category allocate Account for expert
	 * @author zhouqi
	 * @date 2015年8月31日13:31:43
	 * @param String overId
	 * @return Pagination
	 */
	@RequestMapping("allocateAcct")
	@ResponseBody
	public Pagination allocateAcct(String projId, String password){
		Pagination page = new Pagination();
		List<StdExpert> expertList = bidExpertExtractManager.getExpertByProjId(projId);
		SysUser sysUser=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		try{
			for(int i = 0; i < expertList.size(); i++){
				StdExpert stdExpert = expertList.get(i);
				SysRole role = new SysRole();
				SysUser user = new SysUser();
				role.setId(Constants.EXPERT_SYS_ROLE_ID);//专家系统角色
				user.setName(stdExpert.getName());
				user.setEmail(stdExpert.getEmail());
				user.setType(UserType.zj.getKey());
				user.setMain(Whether.yes.getKey());
				user.setPhone(stdExpert.getHandphone());
				user.setStatus(stdExpert.getStatus());
				user.setLocked(Whether.no.getKey());
				user.setPasswordExpired(new Date());
				Long number = userManager.getSequence();
				String sequence = AccountUtil.generate(
						UserType.zj, number);
				user.setUsername(sequence);
				String encodedPwd = DigestUtils.md5Hex(password);//设置默认的密码为123456
				user.setPassword(encodedPwd);
				user.setRole(role);
				user.setUpdUser(sysUser.getName());
				user.setUpdDateTime(new Date());
				if(null!=userManager.getUserById(stdExpert.getExpertId())){
					continue;
				}else{
					user.setId(stdExpert.getExpertId());
					userManager.zxSaveUser(Constants.OP_TYPE_ADD, user, null);
				}
			}	
			page.setSuccess(true);
			page.setMsg("账户分配成功！");
		}catch(Exception e){
			e.printStackTrace();
			page.setSuccess(false);
			page.setMsg("账户分配失败！");
		}
		return page;
	}
	
	

}
